package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.domain.Destination;
import cn.wolfcode.luowowo.domain.StrategyCatalog;
import cn.wolfcode.luowowo.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.IStrategyCatalogService;
import cn.wolfcode.luowowo.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 攻略分类
 */
@Controller
@RequestMapping("/strategyCatalog")
public class StrategyCatalogController {

    @Autowired
    private IStrategyCatalogService strategyCatalogService;


    @Autowired
    private IDestinationService destinationService;


    // 区域显示
    // 这里需要的是分页的数据
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") StrategyCatalogQuery qo){
        Page<StrategyCatalog> page = strategyCatalogService.query(qo);
        model.addAttribute("page",page);
        return "/strategyCatalog/list";
    }


    
    /**
     * 新增和修改
     * 判断对象中是否有Id来确定执行新增还是修改方法
     * @param strategyCatalog
     * @return
     */
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(StrategyCatalog strategyCatalog){
        // 根据传上来的攻略分类对象的deptId 拿到目的地对象
        Destination destination = destinationService.get(strategyCatalog.getDestId());
        // 把目的地对象的name  set回去攻略分类对象中
        strategyCatalog.setDestName(destination.getName());
        // 判断有无Id执行对应的新增或修改方法
        if (StringUtils.hasLength(strategyCatalog.getId())){
            strategyCatalogService.update(strategyCatalog);
        }else{
            strategyCatalogService.save(strategyCatalog);
        }
        return JsonResult.success();
    }


    /**
     * 根据传上来的id删除
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id){
        if (StringUtils.hasLength(id)) {
            strategyCatalogService.delete(id);
        }
        return JsonResult.success();
    }




}
